//
//  RankDetailViewController.m
//  MusicPlayer
//
//  Created by dllo on 15/5/22.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "RankDetailViewController.h"
#import "SANetWorkingTask.h"
#import <UIImageView+WebCache.h>
#import "RankDetailModel.h"
#import "RankDetailTableViewCell.h"
#import "ConnectModel.h"
#import "NovelDataBase.h"
#import "NovelModel.h"
#import "PlayMusicViewController.h"
#import "RecommendLatestModel.h"

@interface RankDetailViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate,ConnectionModelDelegate>
@property (nonatomic ,retain) NSMutableArray *array;
@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, retain) UIScrollView *scroll;
@property (nonatomic, retain) UIImage *image;
@property (nonatomic, retain) NSDictionary *dic;
@property (nonatomic, retain) NSMutableArray *playUrlArray;
@property (nonatomic, retain) UIImageView *myImageView;

@end

@implementation RankDetailViewController

- (void)dealloc
{
    [_array release];
    [_table release];
    [_scroll release];
    [_pageControl release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.array = [NSMutableArray array];
        self.playUrlArray  = [NSMutableArray array];
    }
    return self;
}



-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:YES];
    [self.myImageView removeFromSuperview];
    
    //    self.navigationItem.title = @"音乐库";
    //    self.navigationController.navigationBar.translucent = NO;
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    //
    self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    //
    
    NSUserDefaults *aa = [NSUserDefaults standardUserDefaults];
    
    
    if ([aa objectForKey:@"pic1"]) {
        
        //    if (aa) {
        UIImage *image = [UIImage imageNamed:[aa objectForKey:@"pic1"]];
        
        [_myImageView setImage:image];
    } else {
        [_myImageView setImage:[UIImage imageNamed:@"PIC2.png"]];
        
    }
    
    [self.view addSubview:_myImageView];
        [self.view bringSubviewToFront:self.scroll];
        [self.view bringSubviewToFront:self.table];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIBarButtonItem *exits = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(exitsAction:)];
    self.navigationItem.leftBarButtonItem = exits;
    
    NSString *webSite = [NSString stringWithFormat:@"http://api.dongting.com/channel/ranklist/%@/songs?page=1&bundle_id=com.ttpod.music", self.webId];
    [SANetWorkingTask request:webSite parmater:nil block:^(NSDictionary *result) {

                NSArray *array = [result objectForKey:@"data"];

        
        if (array.count > 0) {
            
            for (NSDictionary *dic in array) {
                RecommendLatestModel *rankDetailModel = [[RecommendLatestModel alloc] init];
                [rankDetailModel setValuesForKeysWithDictionary:dic];
                rankDetailModel.singer_name = rankDetailModel.singerName;
                rankDetailModel.song_name =rankDetailModel.name;
                [self.array addObject:rankDetailModel];
                
                if (rankDetailModel.auditionList.count > 2) {
                    
                    NSDictionary *dic2 = rankDetailModel.auditionList[1];
                    NSString *url = [dic2 objectForKey:@"url"];
                    [self.playUrlArray addObject:url];
                }
                
                
                
            }
        }
        [_table reloadData];
    }];

    [ConnectModel connectWithStr:self.pic_url delegate:self];
    


  
    [self creatTableView];
}

#pragma mark - 返回图片的事件
- (void)exitsAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)connectFinished:(NSData *)data
{
    self.image = [UIImage imageWithData:data];
      [self creatView];
}

- (void)creatView
{
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,200/667.0*self.view.frame.size.height)];
    _scroll.contentSize = CGSizeMake(self.view.frame.size.width * 2, 0);
    _scroll.backgroundColor = [UIColor brownColor];
    _scroll.pagingEnabled = YES;
    _scroll.bounces = NO;
    _scroll.delegate =self;
    _scroll.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_scroll];
    [_scroll release];
    
    
  // 添加滚动视图

    //使用图片初始化背景色
    self.scroll.backgroundColor = [UIColor colorWithPatternImage:self.image];
    //实现模糊效果
    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    visualEffectView.frame = CGRectMake(0, 0, self.view.frame.size.width* 2,self.view.frame.size.width/375 * 200) ;
    visualEffectView.alpha = 1;
    [self.scroll addSubview:visualEffectView];
    [visualEffectView release];

    
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/375*144,self.view.frame.size.width/375* 30, self.view.frame.size.width/375*80, self.view.frame.size.width/375*80)];
    [image sd_setImageWithURL:[NSURL URLWithString:self.pic_url] placeholderImage:nil];
    image.layer.cornerRadius = 9;
    image.clipsToBounds = YES;
    image.alpha = 0.8;
    [visualEffectView addSubview:image];
    [image release];
   
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/375*75,self.view.frame.size.width/375* 120,self.view.frame.size.width/375* 220, self.view.frame.size.width/375*20)];
    titleLabel.text = self.title;
    titleLabel.textAlignment = 1;
    [visualEffectView addSubview:titleLabel];
    [titleLabel release];
    
    UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/375*20 + self.view.frame.size.width, self.view.frame.size.width/375*40, self.view.frame.size.width/375*355 , self.view.frame.size.width/375*120)];
    detailLabel.text = self.details;
    detailLabel.numberOfLines = 0;
    [visualEffectView addSubview:detailLabel];
    [detailLabel release];
    // 添加pageController
    
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(self.view.frame.size.width/375*175, 170/667.0*self.view.frame.size.height,self.view.frame.size.width/375* 20, 20/667.0*self.view.frame.size.height)];
    _pageControl.numberOfPages = 2;
    [self.view addSubview:self.pageControl];
    [self.pageControl release];
    
    
    
    

}
// 创建tableview
- (void)creatTableView
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height/667*200, self.view.frame.size.width, self.view.frame.size.height- self.view.frame.size.height/667* 200-self.view.frame.size.height/667*64 - self.view.frame.size.width/667*150) style:UITableViewStylePlain];
    self.table.backgroundColor = [UIColor clearColor];
    _table.rowHeight = 80;
    _table.delegate =self;
    _table.dataSource = self;
    [self.view addSubview:_table];
    [_table release];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
         static NSString *cellIdentify = @"cell";
     RankDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
   if (!cell) {
    cell = [[[RankDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify] autorelease
            ];
   }
    cell.backgroundColor = [UIColor clearColor];
    RecommendLatestModel *detailModel = [self.array objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.nameLabel.text = detailModel.name;
    cell.nameLabel.textColor = [UIColor whiteColor];
    detailModel.song_name = detailModel.name;
    cell.songLabel.text = detailModel.singerName;
    cell.songLabel.textColor = [UIColor whiteColor];
    cell.songLabel.font = [UIFont systemFontOfSize:12];
    
    cell.num.text = [NSString stringWithFormat:@"%ld.", indexPath.row+ 1];
    cell.num.textColor = [UIColor whiteColor];
    
    self.dic = [_array objectAtIndex:indexPath.row];
    

    return cell;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    RecommendLatestModel *detail = [self.array objectAtIndex:indexPath.row];
    NSString *url = [self.playUrlArray objectAtIndex:indexPath.row];
    
    PlayMusicViewController *playMusic = [[PlayMusicViewController alloc ]init];
    
    playMusic.songName = detail.song_name;
    playMusic.singer = detail.singerName;
    playMusic.songUrl = url;
    playMusic.songArray = self.playUrlArray;
    playMusic.songListArray = self.array;
    playMusic.num = indexPath.row;
    
    [self.navigationController pushViewController:playMusic animated:YES];
    [playMusic release];
    
    
}



//让pagecontrol随scrollview改变
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGPoint point = self.scroll.contentOffset;
    self.pageControl.currentPage = point.x / self.view.frame.size.width;

}




- (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
